<?
/**
 * Copyright 2008 Melange.
 *
 * This file is part of Melange CMS
 *
 * @package cms
 *
 */


/**
 * Requirements
 */
require_once("mvc/action/ActionForm.php");
require_once("mvc/action/ActionMessage.php");
require_once("mvc/action/ActionMessages.php");
require_once("cms/event/om/EventI18n.php");



/**
 * BaseEventForm Class
 *
 * @category    Melange CMS
 * @package     cms
 * @copyright   Copyright (c) 2007 Jeroen Simons. All rights reserved
 * @author      Jeroen Simons <jeroen@melange.nl>
 * @link        http://www.melange.nl/
 */
class BaseEventForm extends ActionForm {


    // ----------------------------------------------------- Instance Variables


    /**
     * eventI18n
     *
     * @var EventI18n 
     */
    protected $eventI18n;


    /**
     * day
     *
     * @var mixed 
     */
    protected $day;


    /**
     * month
     *
     * @var mixed 
     */
    protected $month;


    /**
     * year
     *
     * @var mixed 
     */
    protected $year;


    /**
     * hour
     *
     * @var mixed 
     */
    protected $hour;


    /**
     * minute
     *
     * @var mixed 
     */
    protected $minute;


    /**
     * formImage
     *
     * @var mixed 
     */
    protected $formImage;


    /**
     * deleteImage
     *
     * @var mixed 
     */
    protected $deleteImage;


    // ----------------------------------------------------------- Constructors


    /**
     * Construct new BaseEventForm
     */
    public function __construct() {
        $this->eventI18n = new EventI18n();
        $this->day = null;
        $this->month = null;
        $this->year = null;
        $this->hour = null;
        $this->minute = null;
        $this->formImage = null;
        $this->deleteImage = false;
    }


    // ------------------------------------------------------------- Properties


    /**
     * Get EventI18n
     *
     * @return EventI18n 
     */
    public function getEventI18n() {
        return $this->eventI18n;
    }


    /**
     * Set EventI18n
     *
     * @param EventI18n  eventI18n
     */
    public function setEventI18n(EventI18n $eventI18n) {
        $this->eventI18n = $eventI18n;
    }


    /**
     * Get Day
     *
     * @return mixed 
     */
    public function getDay() {
        return $this->day;
    }


    /**
     * Set Day
     *
     * @param mixed  day
     */
    public function setDay($day) {
        $this->day = $day;
    }


    /**
     * Get Month
     *
     * @return mixed 
     */
    public function getMonth() {
        return $this->month;
    }


    /**
     * Set Month
     *
     * @param mixed  month
     */
    public function setMonth($month) {
        $this->month = $month;
    }


    /**
     * Get Year
     *
     * @return mixed 
     */
    public function getYear() {
        return $this->year;
    }


    /**
     * Set Year
     *
     * @param mixed  year
     */
    public function setYear($year) {
        $this->year = $year;
    }


    /**
     * Get Hour
     *
     * @return mixed 
     */
    public function getHour() {
        return $this->hour;
    }


    /**
     * Set Hour
     *
     * @param mixed  hour
     */
    public function setHour($hour) {
        $this->hour = $hour;
    }


    /**
     * Get Minute
     *
     * @return mixed 
     */
    public function getMinute() {
        return $this->minute;
    }


    /**
     * Set Minute
     *
     * @param mixed  minute
     */
    public function setMinute($minute) {
        $this->minute = $minute;
    }


    /**
     * Get FormImage
     *
     * @return mixed 
     */
    public function getFormImage() {
        return $this->formImage;
    }


    /**
     * Set FormImage
     *
     * @param mixed  formImage
     */
    public function setFormImage($formImage) {
        $this->formImage = $formImage;
    }


    /**
     * Get DeleteImage
     *
     * @return mixed 
     */
    public function getDeleteImage() {
        return $this->deleteImage;
    }


    /**
     * Set DeleteImage
     *
     * @param mixed  deleteImage
     */
    public function setDeleteImage($deleteImage) {
        $this->deleteImage = $deleteImage;
    }


    // --------------------------------------------------------- Public Methods


    /**
     * Validate this form
     *
     * @return array $errors
     */
    public function validate() {

        $errors = new ActionMessages();

        if(is_null($this->getEventI18n()->getName()) || strlen($this->getEventI18n()->getName()) == 0)
            $errors->add("eventI18n->name",
                new ActionMessage("errors.required", true, new ActionMessage("prompt.name", true)));


        if(!is_null($this->getEventI18n()->getName()) && strlen($this->getEventI18n()->getName()) > 0 && strlen($this->getEventI18n()->getName()) > 255)
            $errors->add("eventI18n->name",
                new ActionMessage("errors.maxlength", true, array(
                    new ActionMessage("prompt.name", true),
                    new ActionMessage("255", false))));


        if(is_null($this->getEventI18n()->getIntro()) || strlen($this->getEventI18n()->getIntro()) == 0)
            $errors->add("eventI18n->intro",
                new ActionMessage("errors.required", true, new ActionMessage("prompt.intro", true)));


        if(!is_null($this->getEventI18n()->getEvent()->getUrl()) && strlen($this->getEventI18n()->getEvent()->getUrl()) > 0 && strlen($this->getEventI18n()->getEvent()->getUrl()) > 255)
            $errors->add("eventI18n->event->url",
                new ActionMessage("errors.maxlength", true, array(
                    new ActionMessage("prompt.url", true),
                    new ActionMessage("255", false))));


        if(!is_null($this->getEventI18n()->getEvent()->getUrl()) && strlen($this->getEventI18n()->getEvent()->getUrl()) > 0 && !preg_match('/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i', $this->getEventI18n()->getEvent()->getUrl()))
            $errors->add("eventI18n->event->url",
                new ActionMessage("errors.url", true, new ActionMessage("prompt.url", true)));


        global $ALLOWABLE_IMAGES;
        if(!is_null($this->getFormImage()) && isset($this->formImage["size"]) && $this->formImage["size"] > 0 && !array_key_exists($this->formImage["type"], $ALLOWABLE_IMAGES))
            $errors->add("formImage",
                new ActionMessage("errors.image", true, new ActionMessage("prompt.image", true)));

        return $errors;
    }

}
?>